<?php

namespace ctrl;

use model\article;
use model\article_cate;
use model\siteconfig;
use libs\view;

class content extends base
{
	private static $M, $M_CATE, $upConfig;
	public static function Init()
	{
		parent::init();
		self::$M = new article;
		self::$M_CATE = new article_cate;
		$config =  new siteconfig;
		self::$upConfig = $config->getUpload();
	}

	public static function index()
	{
		if (isset($_GET['list_json'])) {
			jsonList(self::$M->jsonPageSelect());
		} elseif (isset($_GET['catelist'])) { //内容栏目菜单
			$jsonlist = array();
			$jsonlist['code'] = 0;
			$jsonlist['msg'] = '';
			$jsonlist['data'] = ContentCateTree(self::$M_CATE->SelectData(1) ?: '');
			exit(json_encode($jsonlist));
		} else {
			view::assign('catelist', self::$M_CATE->SelectTreeData());
			view::assign('cid', $_GET['cid'] ?? '');
			view::display();
		}
	}


	public static function add()
	{
		if (isset($_GET['view'])) {
			view::assign('upload', self::$upConfig); //获取上传类型
			view::assign('list', self::$M_CATE->SelectTreeData());
			view::assign('info', null);
			view::assign('cid', (int)$_GET['cid']);
			view::assign('infoatt', null);
			view::assign('title', "添加文章");
			view::display("post");
		} else {
			$result = self::$M->insertData();
			Response(array('status' => $result['status'], 'info' => $result['msg'], 'cid' => $result['cid']));
		}
	}

	public static function edit()
	{
		if (isset($_GET['view'])) {
			$info = self::$M->findData();
			view::assign('upload', self::$upConfig); //获取上传类型
			view::assign('list', self::$M_CATE->SelectTreeData());
			view::assign('info', $info['content']);
			view::assign('cid', $info['content']['cid']);
			view::assign('infoatt', $info['attments']);
			view::assign('title', "编辑文章");
			view::display("post");
		} elseif (isset($_GET['switchStatus'])) {
			if (self::$M->switchStatus()) Response(array('status' => 1, 'info' => '设置成功'));
		} elseif (isset($_GET['batchtransfer'])) {
			$result = self::$M->batchTransfer();
			Response(array('info' =>  '成功转移 ' . $result . ' 条'));
		} else {
			$result = self::$M->updateData();
			Response(array('status' => $result['status'], 'info' => $result['msg'], 'cid' => $result['cid']));
		}
	}

	public static function del()
	{
		if (self::$M->deleteData())
			Response(array('status' => 1, 'info' => '删除成功！'));
		else
			Response(array('status' => 0, 'info' => '删除失败！'));
	}
}
